#!/usr/bin/ruby
$:.unshift File.join(File.dirname(__FILE__))
require 'AWS/EC2'
require 'basic_configuration'
require 'aws_context'
require 'pp'

# Adds in IP rule to the given security group.



if ARGV[0] == '-?' || ARGV.size != 5 then
  puts "usage: add-ip-rule group-name tcp|ip|icmp from-port to-port cdir"
  exit 1
end

name=ARGV[0]
protocol=ARGV[1]
from_port=ARGV[2]
to_port=ARGV[3]
cdir=ARGV[4]

# make sure you add:
#   tcp 22 22 0.0.0.0/0


Signal.trap("INT") do
  exit 2
end

config = BasicConfiguration.new
ec2 = AwsContext.instance.ec2(*config.keys)
begin
  ec2.authorize_ingress_by_cidr(name, protocol, from_port, to_port, cdir)
  puts "added rule"
rescue
  puts "error #{$!} #{name}"
  exit 1
end

